class RFValue {

    constructor(fc,gain,freqRange,ppm) {
        this.fc = fc;
        this.gain = gain;
        this.freqRange = freqRange;
        this.ppm = ppm;
    };

    gainRangeOption(port) {
        return {
            from: 0,
            to: 70,
            step: 0.1,
            scale: [0,10,20,30,40,50,60,70],
            format: '%s',
            theme: 'theme-blue',
            width: 100,
            showLabels: false,
            onstatechange: this.fun_onstatechange(port,'gainv'),
            snap: true
        };
    };
    freqRangeOption(port) {
            return {
                from: this.freqRange.from,
                to: this.freqRange.to,
                step: 0.1,
                scale: [0,400,800,1200,1600],
                format: '%s',
                theme: 'theme-blue',
                width: 100,
                showLabels: false,
                onstatechange: this.fun_onstatechange(port,'freqv'),
                snap: true
            };
        };

    set_fc(rp) {
        var port = rp;
        var sid = "#rx_config #rx"+port;
        return (function() {
            var v = $(sid+' #freqv .form-control')[0].value+'e6';
            if(_G.rx_port_on[port]) {
                $.ajax({
                    type: "GET",
                    url: "http://127.0.0.1:5000/set_fc?fc="+v+"&rp="+port,
                    dataType: 'json',
                    success: function (result) {

                    }
                });
            }
        });
    }

    set_ppm(rp) {
        var port = rp;
        var sid = "#rx_config #rx"+port;
        return (function() {
            var v = $(sid+' #ppmv .form-control')[0].value;
            $.ajax({
                type: "GET",
                url: "http://127.0.0.1:5000/ppm?ppm="+v+"&rp="+port,
                dataType: 'json',
                success: function (result) {
                    $('#info').val($('#info').val()+JSON.stringify(result)+"\n");
                }
            });
        });
    }

    set_dppm(rp,dppm) {
        var port = rp;
        var sid = "#rx_config #rx"+port;
        $.ajax({
            type: "GET",
            url: "http://127.0.0.1:5000/ppm?dppm="+dppm+"&rp="+port,
            dataType: 'json',
            success: function (result) {
                $('#info').val($('#info').val()+"set dppm:"+JSON.stringify(result)+"\n");
                var ppm = result.ppm;
                $(sid+' #ppmv .form-control')[0].value = ppm;
            }
        });
    }

    btn_afc(rp) {
        var port = rp;
        return (function() {
            Sdr.current().afc(port);
        });
    }

    fun_onstatechange(port,vdom) {
        var sid = "#rx_config #rx"+port+' #'+vdom+' input.form-control';
        console.log(sid);
        return ((v) => {
            $(sid)[0].value = v.toString();
        });
    }

    start_rtl(port) {
        var fc = $('#freqv .form-control')[0].value+'e6';
        var gain = $('#gainv .form-control')[0].value;
        $.ajax({
            type: "GET",
            url: "http://127.0.0.1:5000/start?rp="+port+"&fc="+fc+"&gain="+gain,
            dataType: 'json',
            success: function (result) {
                if(result.ok) {
                    $("#info").val($("#info").val()+"webc: port:"+port+" start.\n");
                } else {
                    $("#info").val($("#info").val()+"webc: port:"+port+" start return"+result.toString()+".\n");
                }
            }        
        });
    }
    
    stop_rtl(port) {
        $.ajax({
            type: "GET",
            url: "http://127.0.0.1:5000/stop?rp="+port,
            dataType: 'json',
            success: function (result) {
                if(result.ok) {
                    $("#info").val($("#info").val()+"webc: port:"+port+" stop.\n");
                } else {
                    $("#info").val($("#info").val()+"webc: port:"+port+" stop return"+result.toString()+".\n");
                }
            }        
        });
    }

    fun_checkbox_on(rp) {
        var port = rp;
        var sid = "#rx_config #rx"+port;
        return (() => {
            if($(sid+" input#on").is(':checked')) {
                this.start_rtl(port);
                Sdr.current().onEvent('start_rtl',port);
                _G.rx_port_on[port] = true;
            } else {
                this.stop_rtl(port);
                Sdr.current().onEvent('stop_rtl',port);
                _G.rx_port_on[port] = false;
            }
        });        
    }
    
    set_gain(rp) {
        var port = rp;
        var sid = "#rx_config #rx"+port;
        return (function() {
            var v = $(sid+' #gainv .form-control')[0].value;
            if(_G.rx_port_on[port]) {
                $.ajax({
                    type: "GET",
                    url: "http://127.0.0.1:5000/set_gain?gain="+v+"&rp="+port,
                    dataType: 'json',
                    success: function (result) {
    
                    }
                });
            }
        });
    };
    set_rx_view(port) {
        var sid = "#rx_config #rx"+port;
        $(sid+' #gain.single-slider').jRange(this.gainRangeOption(port));
        $(sid+' #gain.single-slider').jRange('setValue',this.gain+'');
        $(sid+' #freq.single-slider').jRange(this.freqRangeOption(port));
        $(sid+' #freq.single-slider').jRange('setValue',this.fc+'');
        $(sid+" input#on").change(this.fun_checkbox_on(port));
        $(sid+' #gainv button').click(this.set_gain(port));
        $(sid+' #freqv button').click(this.set_fc(port));
        $(sid+' #ppmv button#set').click(this.set_ppm(port));
        $(sid+' #ppmv button#afc').click(this.btn_afc(port));
    }
};

function init_rx_view(port,sn) {
    $.ajax({
        type: "GET",
        url: "http://127.0.0.1:5000/rx_port?view=rx_port"+"&rp="+port+"&sn="+sn,
        dataType: 'json',
        success: function (result) {
            if(result.ok) {
                $("#rx_config").append(result.value);
                var rp = result.rp;
                Sdr.current().rf_init_value.set_rx_view(rp);
            }
        }
    });
}
